package com.entrogy.controller;

import com.alibaba.fastjson.JSONObject;
import com.entrogy.bean.PageBean;
import com.entrogy.config.Result;
import com.entrogy.service.LeaveService;
import com.entrogy.utils.ApiReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * xlr
 */
@Api(description = "家长请假功能模块")
@RestController
@RequestMapping(value = "/askforleave")
/*
 * 家长请假流程 1.读取信息 2.先在数据库查询是否有这个人，没有则返回出错信息 3.如果正常返回则将该条请假记录插入数据库，向对应的老师发送一条推送
 * 4.正常返回给前端
 */
public class AskForLeaveController {

	private static final Logger logger = LoggerFactory.getLogger(AskForLeaveController.class);

	@Autowired
    private LeaveService leaveService;

	@ApiOperation(value = "请假页面的初始化")
	@ApiImplicitParam(name = "uid",value = "家长ID",required = true,paramType = "query",dataType = "String")
	@GetMapping("/leaveinit")
	public Result leaveInit(String uid){
		JSONObject returnObj = new JSONObject();
		try {
			returnObj = leaveService.leaveInit(uid);//visitor,parent,teacher
		} catch (Exception e) {
			/*returnObj.put("errcode", 1);
			returnObj.put("errmsg", "请假页面初始化失败");*/

			logger.error("leaveInit:" + uid);
			e.printStackTrace();
			return new Result().getResult(0,"请假页面初始化失败",0,"");
		}
		/*returnObj.put("errcode", 0);
		returnObj.put("errmsg", "ok");*/
		return new Result().getResult(1,"返回成功",1,returnObj);
	}

	@ApiOperation(value = "请假功能的提交")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "uid",value = "家长ID",dataType = "String",paramType = "query",required = true),
			@ApiImplicitParam(name = "s_time",value = "请假开始时间",dataType = "String",paramType = "query",required = true),
			@ApiImplicitParam(name = "e_time",value = "请假结束时间",dataType = "String",paramType = "query",required = true),
			@ApiImplicitParam(name = "content",value = "请假理由",dataType = "String",paramType = "query",required = true),
			@ApiImplicitParam(name = "studentId",value = "学生id",dataType = "String",paramType = "query",required = true)
	})
	@PostMapping("/submitAskForLeave")
	public Result submitAskForLeave(String uid, String s_time,String e_time,String content,Integer studentId) throws ParseException {
		logger.info("请假提交家长id:" + uid + "；开始时间：" + s_time + "；结束时间：" + e_time + "；请假内容：" + content + "；学生id：" + studentId);

		// 判断结束时间是否大于开始时间
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startDate = simpleDateFormat.parse(s_time);
		Date endDate = simpleDateFormat.parse(e_time);
		if (!startDate.before(endDate)){
			return new Result<PageBean>().getResult(402, "对不起，请假开始时间必须大于结束时间！", 2, "");
		}

		// 1.核对学生信息
		JSONObject jsonObject = new JSONObject();
		String result = new String();
		try {
			 result = leaveService.parentAskforLeave(uid, s_time, e_time, content, studentId);
		} catch (Exception e) {
			e.printStackTrace();
			jsonObject.put("errcode", "1");
			jsonObject.put("errmsg", result);
			return new Result().getResult(0,"提交失败！",0,"");
		}
		jsonObject.put("errcode", "0");
		jsonObject.put("errmsg", "ok");
		    return new Result().getResult(1,"提交成功！",1,result);
	}

	@ApiOperation(value = "获取请假页面翻页数据")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "用户ID",value = "uid",dataType = "query",paramType = "String",required = true),
			@ApiImplicitParam(name = "当前页数",value = "currentPage",dataType = "query",paramType = "int",required = true),
            @ApiImplicitParam(name = "每页设定条数",value = "pageSize",dataType = "query",paramType = "int",required = true),
			@ApiImplicitParam(name = "selectType", value = "查询类型，0是全部，1是待审核，2是已审核", paramType = "query", dataType = "String", required = false),
	})
	@RequestMapping(value = "newpage",method = RequestMethod.GET)
	public Result newPage(String uid, int currentPage, int pageSize, Integer selectType){
		JSONObject returnObj = new JSONObject();
		returnObj = leaveService.leaveNewPage(uid, currentPage, pageSize, selectType);
		return new Result().getResult(1,"查询成功！",1,new PageBean<>().pageBeans((List<Object>)(returnObj.get("list")),Integer.parseInt(returnObj.get("total").toString())));
	}

}
